package com.douyang.base.handle;


import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.stp.StpInterface;
import com.douyang.base.client.UserClient;
import com.douyang.base.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class StpInterfaceImpl implements StpInterface {
    @Autowired
    UserClient userClient;


    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        // 返回此 loginId 拥有的权限列表
        return null;
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        // 返回此 loginId 拥有的角色列表
        SaTokenDao dao = SaManager.getSaTokenDao();
        Object object = dao.getObject("user_role:" + loginId.toString());
        List<String>userRoleList=null;
        try {
            if(object == null)throw new RuntimeException();
            userRoleList= (List<String>) object;
        }catch (Exception e){
            Result<List<String>> userRole = userClient.getUserRole(Long.valueOf((String) loginId));
            if(userRole==null || userRole.getCode()!=200)return List.of();
            userRoleList=userRole.getData();
            dao.setObject("user_role:"+loginId.toString(),userRoleList,-1);
        }
        return userRoleList;
    }
}
